package com.china08.yxyapi.api.manage.sch.platform.website.template;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsModule;
import com.china08.yxyapi.entity.db.sws.SwsSchoolTemplet;
import com.china08.yxyapi.entity.db.sws.SwsTempletModule;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsModuleRepository;
import com.china08.yxyapi.repository.db.SwsSchoolTempletRepository;
import com.china08.yxyapi.repository.db.SwsTemplateModuleRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class SwsSchoolModuleListController extends YxyApiBaseController {

    @Autowired
    SwsTemplateModuleRepository swsTemplateModuleRepository;

    @Autowired
    SwsSchoolTempletRepository swsSchoolTempletRepository;

    @Autowired
    SwsModuleRepository swsModuleRepository;

    @ApiOperation("获取模块")
    @RequestMapping(value = "/api_yw/manage/sch/platform/website/module/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response, @RequestBody(required = false) ReqModel reqModel) {

        String templId = reqModel.templId;

        String schoolId = getProfile(request, response).getSchoolId();

        // 所有所有模块
        List<SwsTempletModule> swsTempletModuleList = swsTemplateModuleRepository.findByTemplateId(templId);

        // 学校正在使用的模块
        List<SwsSchoolTemplet> swsSchoolTempletList = swsSchoolTempletRepository.findBySchoolId(schoolId);

        List<RespModel> moduleList = new ArrayList<RespModel>();

        for (SwsTempletModule swsTempletModule : swsTempletModuleList) {

            // 模块id
            String moduleId = swsTempletModule.getModuleId();
            SwsModule swsModule = swsModuleRepository.findById(moduleId);

            RespModel respModel = new RespModel();
            respModel.moduleId = swsTempletModule.getModuleId();
            respModel.templateId = swsTempletModule.getTemplateId();
            respModel.modouleName = swsModule.getModouleName();
            respModel.modouleImage = swsModule.getModouleImage();

            boolean checked = false;
            // 判断是否用使用
            for (SwsSchoolTemplet swsSchoolTemplet : swsSchoolTempletList) {
                checked = swsSchoolTemplet.getModuleId().equals(swsTempletModule.getModuleId());
                if (checked) {
                    break;
                }
            }
            System.out.println(checked);
            respModel.checked = checked;
            moduleList.add(respModel);
        }

        return moduleList;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String templId;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(templId), "模版为空");
        }
    }

    protected class RespModel extends YxyApiRespModel {
        public String templateId;
        public String moduleId;
        public String modouleName;
        public String modouleImage;
        // 前台页面使用
        public boolean checked;
    }
}
